//
//  LREmoticonKeyboard.swift
//  WeiboX
//
//  Created by admin on 11/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit

private let emoticonCellID = "emoticonCellID"

class LREmoticonKeyboard: UIView {
	
	fileprivate lazy var toolBar: LREmoticonToolBar = LREmoticonToolBar()
	
	fileprivate lazy var emoticonCollectionView: UICollectionView = {
		let flowLayout = UICollectionViewFlowLayout()
		flowLayout.scrollDirection = .horizontal
		
		let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
//		collectionView.backgroundColor = self.backgrounColor
		
		collectionView.isPagingEnabled = true
		
		collectionView.showsVerticalScrollIndicator = false
		collectionView.showsHorizontalScrollIndicator = false
		
		collectionView.bounces = false
		
		collectionView.register(LREmoticonCell.self, forCellWithReuseIdentifier: emoticonCellID)
		
		collectionView.dataSource = self
		collectionView.delegate = self
		
		return collectionView
	}()
	
	fileprivate lazy var pageCtrl: UIPageControl = {
		let ctrl = UIPageControl()
		
		ctrl.currentPageIndicatorTintColor = UIColor(patternImage: #imageLiteral(resourceName: "compose_keyboard_dot_selected"))
		ctrl.pageIndicatorTintColor = UIColor(patternImage: #imageLiteral(resourceName: "compose_keyboard_dot_normal"))
		
		return ctrl
	}()

	override init(frame: CGRect) {
		super.init(frame: frame)
		
		setupUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}

	private func setupUI() {
		
		// 默认显示第 1 组第 0 页
		let defaultIndex: IndexPath = IndexPath(item: 0, section: 1)
		
		DispatchQueue.main.async {
			self.setupPageCtrlData(indexPath: defaultIndex)
			
			self.emoticonCollectionView.scrollToItem(at: defaultIndex, at: .left, animated: false)
		}
		
		
		backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
		
		addSubview(emoticonCollectionView)
		addSubview(pageCtrl)
		addSubview(toolBar)
		
		toolBar.snp.makeConstraints { (make) in
			make.leading.bottom.trailing.equalToSuperview()
			make.height.equalTo(35)
		}
		emoticonCollectionView.snp.makeConstraints { (make) in
			make.leading.top.trailing.equalToSuperview()
			make.bottom.equalTo(toolBar.snp.top)
		}
		pageCtrl.snp.makeConstraints { (make) in
			make.bottom.equalTo(toolBar.snp.top)
			make.centerX.equalTo(emoticonCollectionView)
			make.height.equalTo(10)
		}
		
		toolBar.callBack = { [weak self] (type) -> Void in
			let indexPath: IndexPath
			switch type {
			case .recent:
				indexPath = IndexPath(item: 0, section: 0)
			case .normal:
				indexPath = IndexPath(item: 0, section: 1)
			case .emoji:
				indexPath = IndexPath(item: 0, section: 2)
			case .lxh:
				indexPath = IndexPath(item: 0, section: 3)
			}
			self?.emoticonCollectionView.scrollToItem(at: indexPath, at: .left, animated: false)
			
			self?.setupPageCtrlData(indexPath: indexPath)
		}
	}
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
		let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
		
		flowLayout.itemSize = emoticonCollectionView.size
		
		flowLayout.minimumLineSpacing = 0
		flowLayout.minimumInteritemSpacing = 0
	}
	
	// 设置 pageCtrl 的数据
	fileprivate func setupPageCtrlData(indexPath: IndexPath) {
		pageCtrl.numberOfPages = LREmoticonManager.sharedManager.allEmoticonArr[indexPath.section].count
		pageCtrl.currentPage = indexPath.item
	}
}

extension LREmoticonKeyboard: UICollectionViewDelegate, UICollectionViewDataSource {
	internal func numberOfSections(in collectionView: UICollectionView) -> Int {
		return LREmoticonManager.sharedManager.allEmoticonArr.count
	}
	
	internal func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		return LREmoticonManager.sharedManager.allEmoticonArr[section].count
	}
	
	internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: emoticonCellID, for: indexPath) as! LREmoticonCell
		
		cell.backgroundColor = .white
		
		cell.index = indexPath
		cell.emticonArr = LREmoticonManager.sharedManager.allEmoticonArr[indexPath.section][indexPath.item]
		
		return cell
	}
	
	internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
		// 中心点
		let centerX = scrollView.contentOffset.x + emoticonCollectionView.width / 2
		let centerY = emoticonCollectionView.height / 2
		
		let center = CGPoint(x: centerX, y: centerY)
		
		if let indexPath = emoticonCollectionView.indexPathForItem(at: center) {
			let section = indexPath.section
			
			toolBar.setupSelectedStatus(section: section)
			
			setupPageCtrlData(indexPath: indexPath)
		}
	}
}
